package de.uni_potsdam.hpi.bpt.ai.example;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashSet;

import org.json.JSONException;

import de.uni_potsdam.hpi.bpt.ai.collection.Model;
import de.uni_potsdam.hpi.bpt.ai.diagram.Diagram;
import de.uni_potsdam.hpi.bpt.ai.diagram.Node;
import de.uni_potsdam.hpi.bpt.ai.diagram.Shape;

public class ExtractLabels extends Experiment {

	protected FileWriter fstream;
	protected BufferedWriter out;
	
	
	@Override
	protected void examineModel(Model model, Diagram diagram, int index) {
		try {
			int c = 0;
			for (Shape shape : diagram.getShapes()) {
				if (shape instanceof Node) {
					String t = shape.getProperty("title");
					
					if (null == t || t.trim().length() > 0) {
						t = shape.getProperty("name");
					}
					
					if (null != t && t.trim().length() > 0) {
						//out.write(t.trim().toLowerCase()+"\n");
						System.out.println(t.trim().toLowerCase());
					}
					
					c++;
				}
			}
			
			if (0 == c) {
//				System.out.println(model.getName());
			}
			
//			System.out.println(c);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public ExtractLabels() throws IOException {
		this.fstream = new FileWriter(STAT_PATH + File.separator + "labels.txt");
		this.out = new BufferedWriter(fstream);
	}
	
	public void finalize() throws IOException {
		this.out.close();
		this.fstream.close();
	}
	
	public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, JSONException, IOException {
		ExtractLabels exp = new ExtractLabels();
		int num = exp.eachModel(new HashSet<String>(Arrays.asList(new String[]{
			NS_BPMN 
			//NS_EPC
		})));
		System.out.println("\nNumber of analyzed models: " + num);
		exp.finalize();
	}
}
